import os
import sys
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
import importlib.metadata

# 收集fastmcp的所有子模块
hiddenimports = collect_submodules('fastmcp')
# 收集fastmcp的所有数据文件
datas = collect_data_files('fastmcp')

# 尝试找到fastmcp的包元数据目录
try:
    # 获取fastmcp包的路径
    import fastmcp
    fastmcp_path = os.path.dirname(fastmcp.__file__)
    
    # 查找元数据目录
    # 尝试找到METADATA, WHEEL, RECORD等文件
    for parent_dir in [os.path.dirname(fastmcp_path), os.path.dirname(os.path.dirname(fastmcp_path))]:
        dist_info_dirs = [d for d in os.listdir(parent_dir) if d.endswith('.dist-info') and 'fastmcp' in d.lower()]
        for dist_info_dir in dist_info_dirs:
            dist_info_path = os.path.join(parent_dir, dist_info_dir)
            # 添加dist-info目录下的所有文件到datas
            for root, _, files in os.walk(dist_info_path):
                for file in files:
                    file_path = os.path.join(root, file)
                    # 计算相对路径，用于添加到datas
                    rel_path = os.path.relpath(root, parent_dir)
                    datas.append((file_path, rel_path))
    
    # 特殊处理importlib.metadata
    datas.extend(collect_data_files('importlib.metadata'))
except Exception as e:
    print(f"[hook-fastmcp] Error collecting metadata: {e}")
    # 如果出错，继续执行，不中断打包过程